<?php
namespace App\Api\Controllers;
use Common\ApiController;
use Common\Common;
use Common\Models\Material;
use Common\Models\Messages;
use Common\Models\Notices;
use Common\Models\NoticesCate;
use Common\Models\NoticesUsed;

class MessageController extends ApiController
{
	public function initialize()
	{
		parent::initialize();
	}

	/**
	 * 公告列表首页
	 */
	public function mess_listAction(){
	    $MessageDb = new Messages();
	    $result['message_noread'] = $MessageDb->mess_count($this->user_info['id'],['is_read'=>0]);
	    $FNoticesDb = new Notices();
        $result['platform_noread'] = $FNoticesDb->notices_no_read_count($this->user_info['id'],-1);
        $result['comand_noread'] = $FNoticesDb->notices_no_read_count($this->user_info['id'],2);
        $result['comand_notices'] = $FNoticesDb->get_list($this->user_info);
	    $this->ok($result);
	}

	/**
	 * 根据类型获取公告
	 */
	public function mess_typelistAction(){
		if(!isset($this->params['type']) || empty($this->params['type'])){
			$this->params['type']=1;
		}
		if(!isset($this->params['page_size']) || empty($this->params['page_size'])){
			$this->params['page_size']=10;
		}
		if(!isset($this->params['page']) || empty($this->params['page'])){
			$this->params['page']=1;
		}

		switch($this->params['type']){
			case 1:
				$notices = new Notices();
				$list = $notices->findlist(-1,$this->params);
				break;
			case 2:
				$noticesCate = new NoticesCate();
				$cate_ids = $noticesCate->getlistids();
				$notices = new Notices();
				$list = $notices->findlist($cate_ids,$this->params);
				break;
			case 3:
				$message = new Messages();
				$list = $message->list_message($this->params['user_id'],$this->params);
				break;
			default:
				$this->fail(-1,'类型错误');
				break;
		}
		$item_list['items'] = $list;
		foreach($item_list['items'] as $key=>$value){
		    $item_list['items'][$key]['turn_time']=Common::format_date($value['created_at']);
			$item_list['items'][$key]['h5url'] = Common::domain()."/api/h5page/noticedetail?id={$value['id']}&user_id={$this->user_info['id']}";
        }
        $this->ok($item_list['items']);
	}



	/**
	 * 全部标为已读
	 */
	public function allisreadAction(){
		$noticeused = new NoticesUsed();
		$nids = $noticeused->is_used_ids($this->user_info['id']);	//用户已经读取的公告
		$notices = new Notices();
		$nolist = $notices->no_read_notices_list($nids);
		foreach($nolist as $key=>$value){
			$noticesused = new NoticesUsed();
			$noticesused->is_read_used($this->params['user_id'],$value['id']);
		}
		$message = new Messages();
		$data['is_read']=0;
		$mess_list = $message->list_message($this->params['user_id'],$data);
		foreach($mess_list as $key=>$value){
			$message = new Messages();
			$message->is_read_save($value['id']);
		}
		$this->ok([]);
	}


	/**
     * 读取平台活动
     */
	public function noticesUserAction(){
	    if(!isset($this->params['id']) || empty($this->params['id'])){
	        $this->fail(-1,'公告活动id不能为空');
        }
        $Notices = new Notices();
	    $notices = $Notices->noticesIdUser($this->params['id'],$this->params['user_id']);
	    if($notices['status']){
	        $this->ok([]);
        }
        $this->fail(-1,$notices['msg']);
    }

}
